//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.tuliu.appupdate.crash;

import com.tuliu.appupdate.updatelib.IOUtils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.zip.GZIPInputStream;

class MultipartUtility {
    private HttpURLConnection httpConn;
    private OutputStream outputStream;
    private MultipartUtility.UploadCallback listener;

    public MultipartUtility(String requestURL, MultipartUtility.UploadCallback listener) throws IOException {
        this.listener = listener;
        URL url = new URL(requestURL);
        this.httpConn = (HttpURLConnection)url.openConnection();
        this.httpConn.setUseCaches(false);
        this.httpConn.setDoOutput(true);
        this.httpConn.setDoInput(true);
        this.httpConn.setRequestProperty("http.useragent", System.getProperty("http.agent"));
        this.httpConn.setRequestProperty("User-Agent", System.getProperty("http.agent"));
        this.outputStream = this.httpConn.getOutputStream();
    }

    void upload(File file, String key) {
        if(file != null && file.exists()) {
            BufferedInputStream in = null;

            try {
                this.outputStream.write(key.getBytes());
                in = new BufferedInputStream(new FileInputStream(file));
                byte[] e = new byte[8192];

                int read;
                while((read = in.read(e)) != -1) {
                    this.outputStream.write(e, 0, read);
                }
            } catch (FileNotFoundException var10) {
                var10.printStackTrace();
            } catch (IOException var11) {
                var11.printStackTrace();
            } finally {
                IOUtils.closeQuietly(in);
                IOUtils.closeQuietly(this.outputStream);
            }
        }

    }

    void finish() throws IOException {
        String response = null;
        int status = this.httpConn.getResponseCode();
        boolean isSuccess = status == 200;
        if(isSuccess) {
            Object in = this.httpConn.getInputStream();
            String encoding = this.httpConn.getContentEncoding();
            encoding = encoding == null?"UTF-8":encoding;
            if(encoding.contains("gzip")) {
                in = new GZIPInputStream((InputStream)in);
            }

            response = IOUtils.toString((InputStream)in, this.httpConn.getRequestProperty("charset"));
        }

        if(this.listener != null) {
            this.listener.onFinish(response, isSuccess);
        }

    }

    interface UploadCallback {
        void onFinish(String var1, boolean var2);
    }
}
